package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class FrontPage extends JPanel {	
	private static final long serialVersionUID = 8045170813115706395L;

	JPanel lowerPanel, userPanel, namePasswordPanel, titlePanel,
		nameRegisterPanel;
	
	/*titlePanel Components*/
	JLabel title;
	
	/*userPanel Components*/
	JLabel userName, userPassword, userRName, userRPassword, userRMajor,
		userRClasses, userRPasswordV, userREmail;
	JTextField userNameField, userPasswordField, userRNameField,
		userRPasswordField, userRMajorField,
		userRPasswordVField, userREmailField;
	JButton loginButton, registerButton;
	
	/*searchPanel Components*/
	SearchPanel searchPanel;
	
	//Parent Panel
	GUI guiObject;
	
	public void clearFields()
	{
		userNameField.setText("");
		userPasswordField.setText("");
		userRNameField.setText("");
		userRPasswordField.setText("");
		userRMajorField.setText("");
		userRPasswordVField.setText("");
		userREmailField.setText("");
		searchPanel.clearFields();
	}
	
	public FrontPage(GUI guiObject)	{
		setLayout(new GridBagLayout());
		
		this.guiObject = guiObject;
		
		createTitlePanel();
		createLowerPanel();
		
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(0,0,0,0),0,0);

		add(titlePanel, Constraints.gbc);
		
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.SOUTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);

		add(new JSeparator(SwingConstants.HORIZONTAL), Constraints.gbc);

		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.NONE, new Insets(5,0,0,5),0,0);

		add(lowerPanel, Constraints.gbc);
	}
	
	private void createTitlePanel()
	{
		titlePanel = new ImagePanel("Banner-Text-Books.png");
		
		/*title = new JLabel("TEXTBOOK MARKET");
		title.setForeground(Color.white);
		titlePanel.setBackground(Color.blue);
		
		title.setFont(new Font("Serif", Font.BOLD, 48));
		
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0);
		
		titlePanel.add(title, Constraints.gbc);*/
	}
	
	private void createLowerPanel()	{
		lowerPanel = new JPanel(new GridBagLayout());
		lowerPanel.setVisible(true);
		
		createUserPanel();
		searchPanel = new SearchPanel(guiObject, this);
		
		Constraints.setConstraints(0,0,1,1,0,0,GridBagConstraints.WEST,
				GridBagConstraints.NONE,new Insets(0,0,0,0),0,0);

		lowerPanel.add(searchPanel, Constraints.gbc);
		
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0);

		lowerPanel.add(new JSeparator(SwingConstants.VERTICAL), Constraints.gbc);
	
		Constraints.setConstraints(2,0,1,1,0,0,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE,new Insets(0,0,0,0),0,0);

		lowerPanel.add(userPanel, Constraints.gbc);
	}
	
	private void createUserPanel()
	{
		userPanel = new JPanel(new GridBagLayout());
		namePasswordPanel = new JPanel(new GridBagLayout());
		nameRegisterPanel = new JPanel(new GridBagLayout());
		
		userPanel.setVisible(true);
		
		//Making a border then making a title around it.
		Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		TitledBorder title = BorderFactory.createTitledBorder(loweredetched, "Login");
		title.setTitleJustification(TitledBorder.RIGHT);
		namePasswordPanel.setBorder(title);
		
		//Name and text field for text input for Login
		userName = new JLabel("Name", SwingConstants.RIGHT);
		userNameField = new JTextField();
		
		//Setting preferred size of the textfield to 100 pixels wide by 20 high
		userNameField.setPreferredSize(new Dimension(200, 20));
		
		userPassword = new JLabel("Password", JLabel.RIGHT);
		userPasswordField = new JPasswordField();
		userPasswordField.setPreferredSize(new Dimension(200, 20));

		//Setting constraint values and then adding
		//to the userPanel
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		
		namePasswordPanel.add(userName, Constraints.gbc);
		
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);

		namePasswordPanel.add(userNameField, Constraints.gbc);
		
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
	
		namePasswordPanel.add(userPassword, Constraints.gbc);
	
		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);

		namePasswordPanel.add(userPasswordField, Constraints.gbc);
		
		/*--ADDING namePasswordPanel TO userPanel--*/
		
		Constraints.setConstraints(0,0,2,2,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,50,0,0),0,0);

		userPanel.add(namePasswordPanel, Constraints.gbc);
		
		/*--ADDING LOGIN AND REGISTER BUTTON--*/
		
		loginButton = new JButton("Login");
		loginButton.setActionCommand("login");
		loginButton.addActionListener(guiObject);

		Constraints.setConstraints(1,2,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,50,0,3),0,0);

		userPanel.add(loginButton, Constraints.gbc);
		
		
		//Register Panel
		
		//Making a border then making a title around it.
		Border loweredetched2 = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		TitledBorder title2 = BorderFactory.createTitledBorder(loweredetched2, "Register");
		title2.setTitleJustification(TitledBorder.RIGHT);
		nameRegisterPanel.setBorder(title2);

		//Name and text field for text input for Login
		userRName = new JLabel("Name", SwingConstants.RIGHT);
		userRNameField = new JTextField();
		userRNameField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
					GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		nameRegisterPanel.add(userRName, Constraints.gbc);
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		nameRegisterPanel.add(userRNameField, Constraints.gbc);
		
		//password fields
		userRPassword = new JLabel("Password", JLabel.RIGHT);
		userRPasswordField = new JPasswordField();
		userRPasswordField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		nameRegisterPanel.add(userRPassword, Constraints.gbc);
		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		nameRegisterPanel.add(userRPasswordField, Constraints.gbc);

		//Password verification and text field for text input for Login
		userRPasswordV = new JLabel("Verify PW", SwingConstants.RIGHT);
		userRPasswordVField = new JPasswordField();
		userRPasswordVField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,2,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		nameRegisterPanel.add(userRPasswordV, Constraints.gbc);
		Constraints.setConstraints(1,2,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		nameRegisterPanel.add(userRPasswordVField, Constraints.gbc);
		
		//Major and text field for text input for Login
		userRMajor = new JLabel("Major", SwingConstants.RIGHT);
		userRMajorField = new JTextField();
		userRMajorField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,3,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		nameRegisterPanel.add(userRMajor, Constraints.gbc);
		Constraints.setConstraints(1,3,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		nameRegisterPanel.add(userRMajorField, Constraints.gbc);

/*
		//Class and text field for text input for Login
		userRClasses = new JLabel("Classes", SwingConstants.RIGHT);
		userRClassesField = new JTextField();
		userRClassesField.setPreferredSize(new Dimension(100, 20));
		Constraints.setConstraints(0,4,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		nameRegisterPanel.add(userRClasses, Constraints.gbc);
		Constraints.setConstraints(1,4,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		nameRegisterPanel.add(userRClassesField, Constraints.gbc);
*/
		
		//Email and text field for text input for Login
		userREmail = new JLabel("Email", SwingConstants.RIGHT);
		userREmailField = new JTextField();
		userREmailField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,5,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		nameRegisterPanel.add(userREmail, Constraints.gbc);
		Constraints.setConstraints(1,5,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		nameRegisterPanel.add(userREmailField, Constraints.gbc);
		
		//register button
		registerButton = new JButton("Register");
		registerButton.setActionCommand("register");
		registerButton.addActionListener(guiObject);
		Constraints.setConstraints(1,6,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,50,0,3),0,0);
		userPanel.add(registerButton, Constraints.gbc);
		
		/*--ADDING nameRegisterPanel TO userPanel--*/
		Constraints.setConstraints(0,3,2,2,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,50,0,0),0,0);
		userPanel.add(nameRegisterPanel, Constraints.gbc);
	}
}